﻿import QtQuick

Item{
    id: helper

    width: win.width
    height: win.height

    required property Window win
    property real heightTopArea: 75
    property real heightBottomArea: 90

    property int edgeMargin : 6
    property int bw: 3
//    property int edge: 0
    //property var pointer: hh.point    /*.position*/
//    property  bool cursorFreezed:false




    function getCursor(x,y){
        var e = 0

        //const p = Qt.point(mouseX, mouseY);
        const b = bw + edgeMargin; // Increase the corner size slightly
        if (x < b && y < b) return Qt.SizeFDiagCursor;
        if (x >= width - b && y >= height - b) return Qt.SizeFDiagCursor;
        if (x >= width - b && y < b) return Qt.SizeBDiagCursor;
        if (x < b && y >= height - b) return Qt.SizeBDiagCursor;
        if (x < b || x >= width - b) return Qt.SizeHorCursor;
        if (y < b || y >= height - b) return Qt.SizeVerCursor;
    }

    function getEdges(x,y){


        const b = bw + edgeMargin; // Increase the corner size slightly
        let e = 0;
        if (x < b) { e |= Qt.LeftEdge }
        if (x >= width - b) { e |= Qt.RightEdge }
        if (y < b) { e |= Qt.TopEdge }
        if (y >= height - b) { e |= Qt.BottomEdge }
        return e

    }

    function hitMouseArea(x,y){
        const b = bw + edgeMargin - 5
        const rectX = b + edgeMargin
        const rectY = b + heightTopArea
        const rectHeight = win.height - b - heightTopArea - heightBottomArea
        const rectWidth = win.width - b

        if(x > rectX && x < rectWidth
        && y > rectY && y < rectHeight){
            return false
        }

        return true
    }



    MouseArea {
        id:ma
        anchors.fill: parent
        hoverEnabled: true
        preventStealing: true
        property int frozenCursor : Qt.ArrowCursor
        property int edge
        drag.target: ma
        drag.axis: Drag.XAndYAxis
        //drag.threshold: Qt.styleHints.startDragDistance  // 系统推荐阈值

        onPressed: {
            if (win.visibility !== Window.Windowed) return
            frozenCursor = cursorShape
            cursorShape = frozenCursor
            if(hitMouseArea(mouseX,mouseY)){
                if(edge === 0){
                    win.startSystemMove()
                }else{
                    win.startSystemResize(edge)
                }
            }
        }
        //cursorShape
        onReleased: {
            if (win.visibility !== Window.Windowed) return
            if(edge === 0){
                cursorShape = Qt.ArrowCursor
            }else{
                cursorShape = getCursor(mouseX,mouseY)
            }
        }
        //cursorShape
        onPositionChanged: {
            //pressed
            if(win.visibility === Window.Maximized){
                if(hitMouseArea(mouseX,mouseY) && drag.active){
                    win.showNormal()
                    startSystemMove()
                }
            }

            if (win.visibility !== Window.Windowed) return
            edge = getEdges(mouseX,mouseY)
            //console.log("edge = ",edge)

            //hover时
            if(!pressed){
                if(edge === 0){
                    cursorShape = Qt.ArrowCursor
                }else{
                    cursorShape = getCursor(mouseX,mouseY)
                }
            }



        }

        onDoubleClicked: {
            if(hitMouseArea(mouseX,mouseY) && (edge === 0)){
                if(win.visibility === Window.Windowed){
                    win.showMaximized()
                }else if(win.visibility === Window.Maximized){
                    win.showNormal()
                }

            }
        }

        acceptedButtons: Qt.LeftButton // don't handle actual events

    }






}





